package com.mvvm.ktnet.http.base

import android.app.Application
import android.util.Log
import com.alibaba.fastjson.JSONObject
import com.mvvm.ktnet.BuildConfig
import com.mvvm.ktnet.http.okhttploginterceptor.LoggingInterceptor
import com.mvvm.ktnet.http.okhttploginterceptor.Priority
import com.mvvm.ktnet.http.okhttploginterceptor.printer.IPrinter

import nethttp.RxHttp


import okhttp3.OkHttpClient
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.annotation.Converter
import rxhttp.wrapper.annotation.OkClient
import rxhttp.wrapper.cahce.CacheMode
import rxhttp.wrapper.callback.IConverter
import rxhttp.wrapper.converter.FastJsonConverter

import rxhttp.wrapper.converter.MoshiConverter
import rxhttp.wrapper.cookie.CookieStore

import rxhttp.wrapper.ssl.HttpsUtils
import java.io.File
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLSession


/**
 *
 * @ProjectName:    Kotlin_Mvvm
 * @Package:        com.android.alex.http
 * @ClassName:      RxHttpManager
 * @Description:     java类作用描述
 * @Author:         Alex
 * @CreateDate:     2020/7/27 18:07
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/7/27 18:07
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
object RxHttpManager {
    @JvmField
    @Converter(name = "FastJsonConverter")
    var fastJsonConverter: IConverter = FastJsonConverter.create()
    fun init(context: Application) {
        val file = File(context.externalCacheDir, "RxHttpCookie")
        val sslParams = HttpsUtils.getSslSocketFactory()
        val client = OkHttpClient.Builder()
            .cookieJar(CookieStore(file))
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) //添加信任证书
            .hostnameVerifier(HostnameVerifier { hostname: String?, session: SSLSession? -> true }) //忽略host验证
//            .addInterceptor(LogInterceptor())
            .addInterceptor(
                LoggingInterceptor(
                    showLog = BuildConfig.DEBUG,
                    isShowAll = false,
                    tag = "自定义tag",
                    priority = Priority.D
                )
            )
            .addLogInterceptor(BuildConfig.DEBUG)
            .build()
        //RxHttp初始化，自定义OkHttpClient对象
        RxHttp.init(client, BuildConfig.DEBUG)
        //设置缓存策略
        val cacheFile = File(context.externalCacheDir, "RxHttpCache")
        RxHttpPlugins.setCache(
            cacheFile,
            1000 * 100,
            CacheMode.ONLY_NETWORK
        )
//        RxHttpPlugins.setExcludeCacheKeys("time") //设置一些key，不参与cacheKey的组拼
//        设置全局的转换器
//        RxHttp.setConverter(FastJsonConverter.create())

        RxHttp.setConverter(MoshiConverter.create())
        RxHttp.setOnParamAssembly {
            it.addHeader("deviceType", "android")
                .addHeader("versionName", BuildConfig.VERSION_NAME)


        }
    }
}

fun OkHttpClient.Builder.addLogInterceptor(log: Boolean): OkHttpClient.Builder {


    return this
}